﻿<?php require_once('../Connections/conexao.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE subcategorias SET id_categoria=%s, Subcategoria=%s WHERE id_subcategoria=%s",
                       GetSQLValueString($_POST['id_categoria'], "int"),
                       GetSQLValueString($_POST['Subcategoria'], "text"),
                       GetSQLValueString($_POST['id_subcategoria'], "int"));

  mysql_select_db($database_conexao, $conexao);
  $Result1 = mysql_query($updateSQL, $conexao) or die(mysql_error());

  $updateGoTo = "subcategoria.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

$colname_rseditsubcat = "-1";
if (isset($_GET['idsubcategoria'])) {
  $colname_rseditsubcat = $_GET['idsubcategoria'];
}
mysql_select_db($database_conexao, $conexao);
$query_rseditsubcat = sprintf("SELECT * FROM subcategorias WHERE id_subcategoria = %s", GetSQLValueString($colname_rseditsubcat, "int"));
$rseditsubcat = mysql_query($query_rseditsubcat, $conexao) or die(mysql_error());
$row_rseditsubcat = mysql_fetch_assoc($rseditsubcat);
$totalRows_rseditsubcat = mysql_num_rows($rseditsubcat);

mysql_select_db($database_conexao, $conexao);
$query_rscategoria = "SELECT * FROM categorias ORDER BY categoria ASC";
$rscategoria = mysql_query($query_rscategoria, $conexao) or die(mysql_error());
$row_rscategoria = mysql_fetch_assoc($rscategoria);
$totalRows_rscategoria = mysql_num_rows($rscategoria);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>
</head>

<body>
<table width="960" align="center">
  <tr>
    <td colspan="2">
	<?php include ("../temadmin/cabecalho.php");?>

</td>
  </tr>
  <tr>
    <td colspan="2">
	<?php include "../temadmin/menu_vertical.php";
	?>
    </td>
  </tr>
  <tr>
    <td width="250">
	<?php include "../temadmin/menu.php";?>
    </td>
    <td width="698">
   	<table width="99%" align="center">
  <tr>
    <td>      <table width="99%">
        <tr>
          <th scope="col">&nbsp;</th>
          <th scope="col">Editar Subcategoria</th>
          <th scope="col">&nbsp;</th>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
            <table align="center">
              <tr valign="baseline" bgcolor="#999999">
                <td align="right" nowrap="nowrap">Subcategoria:</td>
                <td><input type="text" name="Subcategoria" value="<?php echo htmlentities($row_rseditsubcat['Subcategoria'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
                </tr>
              <tr valign="baseline" bgcolor="#CCCCCC">
                <td align="right" nowrap="nowrap">Categoria:</td>
                <td><select name="id_categoria">
                  <?php 
do {  
?>
                  <option value="<?php echo $row_rscategoria['id_categoria']?>" <?php if (!(strcmp($row_rscategoria['id_categoria'], htmlentities($row_rseditsubcat['id_categoria'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_rscategoria['categoria']?></option>
                  <?php
} while ($row_rscategoria = mysql_fetch_assoc($rscategoria));
?>
                  </select></td>
                </tr>
              <tr valign="baseline" bgcolor="#999999">
                <td colspan="2" align="right" nowrap="nowrap"><input type="submit" value="Atualizar registro" /></td>
                </tr>
              </table>
            <input type="hidden" name="MM_update" value="form1" />
            <input type="hidden" name="id_subcategoria" value="<?php echo $row_rseditsubcat['id_subcategoria']; ?>" />
            <input type="hidden" name="MM_update" value="form1" />
          </form></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        </table>
      
    </td>
  </tr>
</table>

    </td>
  </tr>
  <tr>
    <td colspan="2">
	<?php include ("../temadmin/rodape.php");?>
    </td>
  </tr>
</table>

<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($rseditsubcat);

mysql_free_result($rscategoria);
?>
